/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package ep.persistence.controllers;

import ep.persistence.controllers.exceptions.IllegalOrphanException;
import ep.persistence.controllers.exceptions.NonexistentEntityException;
import ep.persistence.entities.T005Patient;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.persistence.Query;
import javax.persistence.EntityNotFoundException;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;
import ep.persistence.entities.T004Insurance;
import ep.persistence.entities.T005T007;
import java.util.ArrayList;
import java.util.Collection;
import ep.persistence.entities.T006Prescription;

/**
 *
 * @author Spyros
 */
public class T005PatientJpaController {

    public T005PatientJpaController() {
        emf = Persistence.createEntityManagerFactory("EprescPersistencePU");
    }
    private EntityManagerFactory emf = null;

    public EntityManager getEntityManager() {
        return emf.createEntityManager();
    }

    public void create(T005Patient t005Patient) {
        if (t005Patient.getT005T007Collection() == null) {
            t005Patient.setT005T007Collection(new ArrayList<T005T007>());
        }
        if (t005Patient.getT006PrescriptionCollection() == null) {
            t005Patient.setT006PrescriptionCollection(new ArrayList<T006Prescription>());
        }
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            T004Insurance t004Insurance = t005Patient.getT004Insurance();
            if (t004Insurance != null) {
                t004Insurance = em.getReference(t004Insurance.getClass(), t004Insurance.getId());
                t005Patient.setT004Insurance(t004Insurance);
            }
            Collection<T005T007> attachedT005T007Collection = new ArrayList<T005T007>();
            for (T005T007 t005T007CollectionT005T007ToAttach : t005Patient.getT005T007Collection()) {
                t005T007CollectionT005T007ToAttach = em.getReference(t005T007CollectionT005T007ToAttach.getClass(), t005T007CollectionT005T007ToAttach.getId());
                attachedT005T007Collection.add(t005T007CollectionT005T007ToAttach);
            }
            t005Patient.setT005T007Collection(attachedT005T007Collection);
            Collection<T006Prescription> attachedT006PrescriptionCollection = new ArrayList<T006Prescription>();
            for (T006Prescription t006PrescriptionCollectionT006PrescriptionToAttach : t005Patient.getT006PrescriptionCollection()) {
                t006PrescriptionCollectionT006PrescriptionToAttach = em.getReference(t006PrescriptionCollectionT006PrescriptionToAttach.getClass(), t006PrescriptionCollectionT006PrescriptionToAttach.getId());
                attachedT006PrescriptionCollection.add(t006PrescriptionCollectionT006PrescriptionToAttach);
            }
            t005Patient.setT006PrescriptionCollection(attachedT006PrescriptionCollection);
            em.persist(t005Patient);
            if (t004Insurance != null) {
                t004Insurance.getT005PatientCollection().add(t005Patient);
                t004Insurance = em.merge(t004Insurance);
            }
            for (T005T007 t005T007CollectionT005T007 : t005Patient.getT005T007Collection()) {
                T005Patient oldT005PatientOfT005T007CollectionT005T007 = t005T007CollectionT005T007.getT005Patient();
                t005T007CollectionT005T007.setT005Patient(t005Patient);
                t005T007CollectionT005T007 = em.merge(t005T007CollectionT005T007);
                if (oldT005PatientOfT005T007CollectionT005T007 != null) {
                    oldT005PatientOfT005T007CollectionT005T007.getT005T007Collection().remove(t005T007CollectionT005T007);
                    oldT005PatientOfT005T007CollectionT005T007 = em.merge(oldT005PatientOfT005T007CollectionT005T007);
                }
            }
            for (T006Prescription t006PrescriptionCollectionT006Prescription : t005Patient.getT006PrescriptionCollection()) {
                T005Patient oldT005PatientOfT006PrescriptionCollectionT006Prescription = t006PrescriptionCollectionT006Prescription.getT005Patient();
                t006PrescriptionCollectionT006Prescription.setT005Patient(t005Patient);
                t006PrescriptionCollectionT006Prescription = em.merge(t006PrescriptionCollectionT006Prescription);
                if (oldT005PatientOfT006PrescriptionCollectionT006Prescription != null) {
                    oldT005PatientOfT006PrescriptionCollectionT006Prescription.getT006PrescriptionCollection().remove(t006PrescriptionCollectionT006Prescription);
                    oldT005PatientOfT006PrescriptionCollectionT006Prescription = em.merge(oldT005PatientOfT006PrescriptionCollectionT006Prescription);
                }
            }
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void edit(T005Patient t005Patient) throws IllegalOrphanException, NonexistentEntityException, Exception {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            T005Patient persistentT005Patient = em.find(T005Patient.class, t005Patient.getId());
            T004Insurance t004InsuranceOld = persistentT005Patient.getT004Insurance();
            T004Insurance t004InsuranceNew = t005Patient.getT004Insurance();
            Collection<T005T007> t005T007CollectionOld = persistentT005Patient.getT005T007Collection();
            Collection<T005T007> t005T007CollectionNew = t005Patient.getT005T007Collection();
            Collection<T006Prescription> t006PrescriptionCollectionOld = persistentT005Patient.getT006PrescriptionCollection();
            Collection<T006Prescription> t006PrescriptionCollectionNew = t005Patient.getT006PrescriptionCollection();
            List<String> illegalOrphanMessages = null;
            for (T005T007 t005T007CollectionOldT005T007 : t005T007CollectionOld) {
                if (!t005T007CollectionNew.contains(t005T007CollectionOldT005T007)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain T005T007 " + t005T007CollectionOldT005T007 + " since its t005Patient field is not nullable.");
                }
            }
            for (T006Prescription t006PrescriptionCollectionOldT006Prescription : t006PrescriptionCollectionOld) {
                if (!t006PrescriptionCollectionNew.contains(t006PrescriptionCollectionOldT006Prescription)) {
                    if (illegalOrphanMessages == null) {
                        illegalOrphanMessages = new ArrayList<String>();
                    }
                    illegalOrphanMessages.add("You must retain T006Prescription " + t006PrescriptionCollectionOldT006Prescription + " since its t005Patient field is not nullable.");
                }
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            if (t004InsuranceNew != null) {
                t004InsuranceNew = em.getReference(t004InsuranceNew.getClass(), t004InsuranceNew.getId());
                t005Patient.setT004Insurance(t004InsuranceNew);
            }
            Collection<T005T007> attachedT005T007CollectionNew = new ArrayList<T005T007>();
            for (T005T007 t005T007CollectionNewT005T007ToAttach : t005T007CollectionNew) {
                t005T007CollectionNewT005T007ToAttach = em.getReference(t005T007CollectionNewT005T007ToAttach.getClass(), t005T007CollectionNewT005T007ToAttach.getId());
                attachedT005T007CollectionNew.add(t005T007CollectionNewT005T007ToAttach);
            }
            t005T007CollectionNew = attachedT005T007CollectionNew;
            t005Patient.setT005T007Collection(t005T007CollectionNew);
            Collection<T006Prescription> attachedT006PrescriptionCollectionNew = new ArrayList<T006Prescription>();
            for (T006Prescription t006PrescriptionCollectionNewT006PrescriptionToAttach : t006PrescriptionCollectionNew) {
                t006PrescriptionCollectionNewT006PrescriptionToAttach = em.getReference(t006PrescriptionCollectionNewT006PrescriptionToAttach.getClass(), t006PrescriptionCollectionNewT006PrescriptionToAttach.getId());
                attachedT006PrescriptionCollectionNew.add(t006PrescriptionCollectionNewT006PrescriptionToAttach);
            }
            t006PrescriptionCollectionNew = attachedT006PrescriptionCollectionNew;
            t005Patient.setT006PrescriptionCollection(t006PrescriptionCollectionNew);
            t005Patient = em.merge(t005Patient);
            if (t004InsuranceOld != null && !t004InsuranceOld.equals(t004InsuranceNew)) {
                t004InsuranceOld.getT005PatientCollection().remove(t005Patient);
                t004InsuranceOld = em.merge(t004InsuranceOld);
            }
            if (t004InsuranceNew != null && !t004InsuranceNew.equals(t004InsuranceOld)) {
                t004InsuranceNew.getT005PatientCollection().add(t005Patient);
                t004InsuranceNew = em.merge(t004InsuranceNew);
            }
            for (T005T007 t005T007CollectionNewT005T007 : t005T007CollectionNew) {
                if (!t005T007CollectionOld.contains(t005T007CollectionNewT005T007)) {
                    T005Patient oldT005PatientOfT005T007CollectionNewT005T007 = t005T007CollectionNewT005T007.getT005Patient();
                    t005T007CollectionNewT005T007.setT005Patient(t005Patient);
                    t005T007CollectionNewT005T007 = em.merge(t005T007CollectionNewT005T007);
                    if (oldT005PatientOfT005T007CollectionNewT005T007 != null && !oldT005PatientOfT005T007CollectionNewT005T007.equals(t005Patient)) {
                        oldT005PatientOfT005T007CollectionNewT005T007.getT005T007Collection().remove(t005T007CollectionNewT005T007);
                        oldT005PatientOfT005T007CollectionNewT005T007 = em.merge(oldT005PatientOfT005T007CollectionNewT005T007);
                    }
                }
            }
            for (T006Prescription t006PrescriptionCollectionNewT006Prescription : t006PrescriptionCollectionNew) {
                if (!t006PrescriptionCollectionOld.contains(t006PrescriptionCollectionNewT006Prescription)) {
                    T005Patient oldT005PatientOfT006PrescriptionCollectionNewT006Prescription = t006PrescriptionCollectionNewT006Prescription.getT005Patient();
                    t006PrescriptionCollectionNewT006Prescription.setT005Patient(t005Patient);
                    t006PrescriptionCollectionNewT006Prescription = em.merge(t006PrescriptionCollectionNewT006Prescription);
                    if (oldT005PatientOfT006PrescriptionCollectionNewT006Prescription != null && !oldT005PatientOfT006PrescriptionCollectionNewT006Prescription.equals(t005Patient)) {
                        oldT005PatientOfT006PrescriptionCollectionNewT006Prescription.getT006PrescriptionCollection().remove(t006PrescriptionCollectionNewT006Prescription);
                        oldT005PatientOfT006PrescriptionCollectionNewT006Prescription = em.merge(oldT005PatientOfT006PrescriptionCollectionNewT006Prescription);
                    }
                }
            }
            em.getTransaction().commit();
        } catch (Exception ex) {
            String msg = ex.getLocalizedMessage();
            if (msg == null || msg.length() == 0) {
                Integer id = t005Patient.getId();
                if (findT005Patient(id) == null) {
                    throw new NonexistentEntityException("The t005Patient with id " + id + " no longer exists.");
                }
            }
            throw ex;
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public void destroy(Integer id) throws IllegalOrphanException, NonexistentEntityException {
        EntityManager em = null;
        try {
            em = getEntityManager();
            em.getTransaction().begin();
            T005Patient t005Patient;
            try {
                t005Patient = em.getReference(T005Patient.class, id);
                t005Patient.getId();
            } catch (EntityNotFoundException enfe) {
                throw new NonexistentEntityException("The t005Patient with id " + id + " no longer exists.", enfe);
            }
            List<String> illegalOrphanMessages = null;
            Collection<T005T007> t005T007CollectionOrphanCheck = t005Patient.getT005T007Collection();
            for (T005T007 t005T007CollectionOrphanCheckT005T007 : t005T007CollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This T005Patient (" + t005Patient + ") cannot be destroyed since the T005T007 " + t005T007CollectionOrphanCheckT005T007 + " in its t005T007Collection field has a non-nullable t005Patient field.");
            }
            Collection<T006Prescription> t006PrescriptionCollectionOrphanCheck = t005Patient.getT006PrescriptionCollection();
            for (T006Prescription t006PrescriptionCollectionOrphanCheckT006Prescription : t006PrescriptionCollectionOrphanCheck) {
                if (illegalOrphanMessages == null) {
                    illegalOrphanMessages = new ArrayList<String>();
                }
                illegalOrphanMessages.add("This T005Patient (" + t005Patient + ") cannot be destroyed since the T006Prescription " + t006PrescriptionCollectionOrphanCheckT006Prescription + " in its t006PrescriptionCollection field has a non-nullable t005Patient field.");
            }
            if (illegalOrphanMessages != null) {
                throw new IllegalOrphanException(illegalOrphanMessages);
            }
            T004Insurance t004Insurance = t005Patient.getT004Insurance();
            if (t004Insurance != null) {
                t004Insurance.getT005PatientCollection().remove(t005Patient);
                t004Insurance = em.merge(t004Insurance);
            }
            em.remove(t005Patient);
            em.getTransaction().commit();
        } finally {
            if (em != null) {
                em.close();
            }
        }
    }

    public List<T005Patient> findT005PatientEntities() {
        return findT005PatientEntities(true, -1, -1);
    }

    public List<T005Patient> findT005PatientEntities(int maxResults, int firstResult) {
        return findT005PatientEntities(false, maxResults, firstResult);
    }

    private List<T005Patient> findT005PatientEntities(boolean all, int maxResults, int firstResult) {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            cq.select(cq.from(T005Patient.class));
            Query q = em.createQuery(cq);
            if (!all) {
                q.setMaxResults(maxResults);
                q.setFirstResult(firstResult);
            }
            return q.getResultList();
        } finally {
            em.close();
        }
    }

    public T005Patient findT005Patient(Integer id) {
        EntityManager em = getEntityManager();
        try {
            return em.find(T005Patient.class, id);
        } finally {
            em.close();
        }
    }

    public int getT005PatientCount() {
        EntityManager em = getEntityManager();
        try {
            CriteriaQuery cq = em.getCriteriaBuilder().createQuery();
            Root<T005Patient> rt = cq.from(T005Patient.class);
            cq.select(em.getCriteriaBuilder().count(rt));
            Query q = em.createQuery(cq);
            return ((Long) q.getSingleResult()).intValue();
        } finally {
            em.close();
        }
    }

}
